<?php
class ClassMail 
{
   var $to;
   var $form;
   var $cc;
   var $bcc;
   var $subject;
   var $message;
   var $contentType;
   var $smtp;
   var $smtp_port;
   var $sendmail_from;
   
   function ClassMail() //init
   {
       $this->smtp				=	 ""	;
	   $this->smtp_port			=	 ""	;
	   $this->sendemail_from	=	 ""	;
	   $this->to        		=    ""	;
       $this->from    			=    ""	;
       $this->cc    			=    ""	;
       $this->bcc    			=    ""	;
       $this->subject   		=    ""	;
       $this->message   		=    ""	;
       $this->contentType 		= 	 ""	; // text, html
   }
   
   function _checkNumberOnly( $port )
   {
		if(ereg("^[0-9-]+$", $port)) return true  ;
		else return false ;
   }
   
   function _isEmail($email)
   {
       // regx to test for valid e-mail adres
       $regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$';
       if (eregi($regex, $email)) return true;
       else return false;
   }
   
   function _isSMTP($string)
   {
       // regx to test for valid smtp
       $regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\.[a-z0-9-]+)(\.[a-z0-9-]{2,})+$';
       if (eregi($regex, $string)) return true;
       else return false;
   }
   
   function _sendMail( $uni )
   {
       //check if smtp are valid
	   if (!ClassMail::_isEmail($this->sendemail_from)) die('ERROR: Invalid sending e-mail address');
	   if (!ClassMail::_isSMTP($this->smtp)) die('ERROR: Invalid SMTP');
	   if (!ClassMail::_checkNumberOnly($this->smtp_port)) die('ERROR: Invalid Port. It has to be in number!');
	   
	   ini_set("SMTP", $this->smtp);
	   ini_set("SMTP_PORT", $this->smtp_port);
	   ini_set("sendmail_from", $this->sendemail_from);
	   
	   // check if e-mail adresses are valid.
       //if (!ClassMail::_isEmail($this->to)) die('ERROR: Invalid To e-mail address');
       //if (!ClassMail::_isEmail($this->from)) die('ERROR: Invalid From e-mail address');
       if (!ClassMail::_isEmail($this->cc) && !$this->cc=="") die('ERROR: Invalid CC e-mail address');
       if (!ClassMail::_isEmail($this->bcc) && !$this->bcc=="") die('ERROR: Invalid BCC e-mail address');
       
       // To send HTML mail, you can set the Content-type header. html is the default
       $headers  = "MIME-Version: 1.0\r\n";
       if ($this->contentType=="html") $headers .= "Content-type: text/html; charset=".$uni."\r\n";
       else $headers .= "Content-type: text/plain; charset=".$uni."\r\n";
       
       // additional headers  for From, Cc and Bcc
	   $headers .= "From: ".$this->from."\r\n";
       if (!$this->cc=="")  $headers .= "Cc: ".$this->cc."\r\n";
       if (!$this->bcc=="") $headers .= "Bcc: ".$this->bcc."\r\n";
       
       // send the e-mail
       return mail($this->to, $this->subject, $this->message, $headers);
   }
}
?>
